package com.cl.cloud.binlog.handler;

import com.cl.cloud.binlog.content.BinLogApplicationContext;
import com.cl.core.redis.lock.LockType;
import com.cl.core.redis.lock.RedisLockClient;
import org.redisson.api.RLock;

/**
 * @description: 基于redis实现的binlog分布式客户端处理器
 * @author: liuzijian
 * @time: 2022-07-08 16:00
 */
public class RedisDistributedClientHandler extends AbstractDistributedClientHandler {

    private final RedisLockClient redissonClient;

    public RedisDistributedClientHandler(BinLogApplicationContext context, RedisLockClient redissonClient) {
        super(context);
        this.redissonClient = redissonClient;
    }

    @Override
    boolean requireClient(String key) {
        RLock lock = redissonClient.getLock(key, LockType.FAIR);
        return lock.tryLock();
    }

    @Override
    void releasClient(String key) {
        RLock lock = redissonClient.getLock(key, LockType.FAIR);
        lock.unlock();
    }
}
